package com.mobilekites.gcm;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.Log;

import com.mobilekites.MobileKitesApp;
import com.mobilekites.R;
import com.mobilekites.util.KitesPrefers.AdOldPrefers;

public class UpdateController {
	private static final String TAG = "UpdateController";

	private JSONObject mJsonObject;
	private int mClientVersion;
	private Context mContext;
	private AdOldPrefers mAdOldPrefers;

	public static interface OnUpdateSelected {
		public void onAcceptUpdate();

		public void onIgnoreUpdate();
	}

	public UpdateController(Context context) {
		mClientVersion = getClientVerion(context);
		mContext = context;
		mAdOldPrefers = MobileKitesApp.get().getAdOldPrefers();
	}

	public UpdateController(Context context, JSONObject data) {
		this(context);
		mJsonObject = data;
	}

	public void saveInfo() {
		try {
			int forceupdate = mJsonObject.getInt("forceupdate");
			int versionServer = mJsonObject.getInt("version");
			String url = mJsonObject.getString("url");
			// int type = mJsonObject.getInt("updatetype");
			String message = mJsonObject.getString("message");

			if (versionServer > mClientVersion) {
				mAdOldPrefers.setUpdate(true);
			} else {
				mAdOldPrefers.setUpdate(false);
			}
			mAdOldPrefers.setAppVersionServer(versionServer);
			mAdOldPrefers.setUpdateMessage(message);
			mAdOldPrefers.setForceUpdate(forceupdate);
			mAdOldPrefers.setUpdateURL(url);
			// mPreferences.setUpdateType(type);

			Log.i(TAG, "Save version info success");

		} catch (JSONException e) {
			e.printStackTrace();
			Log.e(TAG, "Save version info fail");
		}
	}

	public void execute() {
		int serverVersion = mAdOldPrefers.getAppVersionServer();
		if (serverVersion > mClientVersion) {
			AlertDialog.Builder builder = new Builder(mContext);
			builder.setTitle(R.string.kites_notify);
			builder.setMessage(mAdOldPrefers.getUpdateMessage());

			if (mAdOldPrefers.isForceUpdate() == 1) {
				builder.setCancelable(false);
			} else {
				builder.setNegativeButton(R.string.kites_no, null);
			}
			builder.setPositiveButton(R.string.kites_ok, new OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setData(Uri.parse(mAdOldPrefers.getUpdateURL()));
					mContext.startActivity(intent);
				}
			});
			builder.show();
		}
	}

	public void execute(final Activity activity,
			DialogInterface.OnClickListener onClickListener,
			OnUpdateSelected onUpdateSelected) {
		if (mAdOldPrefers.hasUpdate()) {
			AlertDialog.Builder builder = new Builder(activity);
			builder.setTitle(R.string.kites_notify);
			builder.setMessage(mAdOldPrefers.getUpdateMessage());
			builder.setCancelable(false);
			if (mAdOldPrefers.isForceUpdate() != 1) {
				builder.setNegativeButton(R.string.kites_no, onClickListener);
			}
			builder.setPositiveButton(R.string.kites_ok,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							Intent intent = new Intent(Intent.ACTION_VIEW);
							intent.setData(Uri.parse(mAdOldPrefers
									.getUpdateURL()));
							mContext.startActivity(intent);
						}
					});
			builder.show();
		} else {
			onUpdateSelected.onIgnoreUpdate();
		}
	}

	private int getClientVerion(Context context) {
		int version = 1;
		PackageInfo pInfo;
		try {
			pInfo = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			version = pInfo.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return version;
	}
}
